﻿using PHMEE.Entities.DaqInfo.TdDevices;
using PHMEE.IRepositories.DaqInfo.TdDevices;
using PHMEE.ToolKits;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;
using static PHMEE.Constants.PhmeeEnum;

namespace PHMEE
{
    /// <summary>
    /// 
    /// </summary>
    public class TdDeviceManager : DomainService, ITdDeviceManager
    {
        private readonly ITdDeviceTypeRepository _tdDeviceTypeRepository;
        private readonly ITdDeviceAttributeRepository _tdDeviceAttributeRepository;
        private readonly ITdDeviceInstanceRepository _tdDeviceInstanceRepository;
        private readonly ITdDevicePointRelationRepository _tdDevicePointRelationRepository;
        public TdDeviceManager(
            ITdDeviceTypeRepository tdDeviceTypeRepository,
            ITdDeviceInstanceRepository tdDeviceInstanceRepository,
            ITdDeviceAttributeRepository tdDeviceAttributeRepository,
            ITdDevicePointRelationRepository tdDevicePointRelationRepository)
        {
            _tdDeviceTypeRepository = tdDeviceTypeRepository;
            _tdDeviceInstanceRepository = tdDeviceInstanceRepository;
            _tdDeviceAttributeRepository = tdDeviceAttributeRepository;
            _tdDevicePointRelationRepository = tdDevicePointRelationRepository;
        }
        /// <summary>
        /// 根据超级表或子表ID获取属性
        /// </summary>
        /// <param name="tableId"></param>
        /// <param name="isSuperTable"></param>
        /// <returns></returns>

        public async Task<List<TdDeviceAttribute>> GetAttributes(Guid tableId, bool isSuperTable)
        {
            Guid superTableId;

            if (isSuperTable)
            {
                // 验证 tableId 是否为一个有效的超级表 ID
                var superTable = await _tdDeviceTypeRepository.FirstOrDefaultAsync(st => st.Id == tableId);
                if (superTable == null)
                {
                    throw new UserFriendlyException("未发现当前超级表", "指定的超级表不存在.")
                        .WithData("TableId", tableId);
                }
                superTableId = tableId;
            }
            else
            {
                // 如果是子表，先查找其对应的超级表 ID
                var subTable = await _tdDeviceInstanceRepository.FirstOrDefaultAsync(st => st.Id == tableId);
                if (subTable == null)
                {
                    throw new UserFriendlyException("未发现当前子表", "指定的子表不存在.")
                        .WithData("TableId", tableId);
                }
                superTableId = subTable.TdDeviceTypeId;
            }

            // 获取超级表的属性
            var attributes = await _tdDeviceAttributeRepository.GetAttributesBySuperTableIdAsync(superTableId);

            return attributes;
        }

        /// <summary>
        /// 根据测点 ID 获取与之关联的子表关系
        /// </summary>
        /// <param name="pointId"></param>
        /// <returns></returns>
        public async Task<TdDevicePointRelation> GetRelationAsync(Guid pointId)
        {
            return await _tdDevicePointRelationRepository.GetRelationByPointIdAsync(pointId);
        }

        /// <summary>
        /// 获取子表实例集合。
        /// </summary>
        /// <param name="pointIds">测点集合</param>
        /// <returns></returns>
        public async Task<List<TdDevicePointRelation>> GetRelationsAsync(List<Guid> pointIds)
        {
            return await _tdDevicePointRelationRepository.GetRelationsAsync(pointIds);
        }
        /// <summary>
        /// 获取TdEngine数据类型集合
        /// </summary>
        /// <returns></returns>
        public Dictionary<int, object> GetTdEngineAttributeDataTypes()
        {
            return PHMEECoreHelper.PhmeeGetEnumKeyDescriptions<TdEngineAttributeEnum>();
        }
    }
}
